﻿namespace Skymate.Mvc.Models
{
    using System;

    using Abp.Web.Models;

    using Skymate.Engines;

    /// <inheritdoc/>
    public class ErrorInfoBuilder : IErrorInfoBuilder, ISingletonDependency
    {
        /// <inheritdoc/>
        public static IErrorInfoBuilder Instance
        {
            get
            {
                return EngineContext.Current.Resolve<IErrorInfoBuilder>();
            }
        }

        private IExceptionToErrorInfoConverter Converter { get; set; }

        /// <inheritdoc/>
        public ErrorInfoBuilder(/*IAbpWebModuleConfiguration configuration*/)
        {

            this.Converter = new DefaultErrorInfoConverter(/*configuration*/);

        }

        /// <inheritdoc/>
        public ErrorInfo BuildForException(Exception exception)
        {
            return this.Converter.Convert(exception);
        }

        /// <summary>
        /// Adds an exception converter that is used by <paramref name="ForException"/> method.
        /// </summary>
        /// <param name="converter">Converter object</param>
        public void AddExceptionConverter(IExceptionToErrorInfoConverter converter)
        {
            converter.Next = this.Converter;
            this.Converter = converter;
        }
    }
}
